<?php
/**
 * +--------------------------------------------------------+
 * @Created by Phpstorm.
 * @Category StockArticleRepository.php
 * @Depiction
 * @Author hwy <429208948@qq.com>
 * @Date 2022/4/17  17:02
 * +--------------------------------------------------------+
 */

namespace App\Repositories;

use App\Models\StockArticle;

class StockArticleRepository extends BaseRepository
{
    protected $stockArticle;

    public function __construct(StockArticle $stockArticle)
    {
        $this->stockArticle = $stockArticle;
    }

    /**
     * @param $params
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/18  14:19
     */
    public function getList($params)
    {
        $query = $this->stockArticle->query();

        $perPage = $this->perPage($params);
        [$sort, $order] = $this->paramsSet($params);

        if (!empty($params['top'])) {
            $query = $query->where('openStick', $params['top']);
        }
        if (isset($params['isOpen'])) {
            $query = $query->where('isOpen', $params['isOpen']);
        }
        if (!empty($params['category'])) {
            $query = $query->where('zlName', $params['category']);
        }
        if (!empty($params['title'])) {
            $query = $query->where('content', 'like', '%' . $params['title'] . '%')
                ->orWhere('summary', 'like', '%' . $params['title'] . '%')
                ->orWhere('title', 'like', '%' . $params['title'] . '%');
        }

        return $list = $query->orderBy($sort, $order)->paginate($perPage);
    }

    /**
     * @param $params
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/18  14:21
     */
    public function getInfo($params)
    {
        $data = $this->stockArticle->query()->with('has_one_article_stock_content')->where('id', $params['id'])->first();

        return $data;
    }

    public function getListByLimit($limit = 20)
    {
        $data = $this->stockArticle->query()
            ->select('subjectId', 'type')
            ->orderBy('id', 'desc')
            ->limit($limit)
            ->get();

        return $data;
    }


    public function addData($params)
    {
        $res = $this->stockArticle->query()->insert($params);

        return $res;
    }

}
